<template>
    <div>
        <!-- 搜索框 -->
        <div class="search-from">
            <div class="search-input">
                <div class="search-icon">
                    <span>
                        <svg t="1640001138274" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="29105" width="20" height="20"><path d="M830.486464 796.124515 672.790943 638.42797c44.959904-52.799318 72.109099-121.232412 72.109099-196.016087 0-167.084182-135.448007-302.533214-302.53219-302.533214s-302.533214 135.449031-302.533214 302.533214 135.449031 302.53219 302.533214 302.53219c74.782651 0 143.215745-27.149196 196.017111-72.109099L796.101988 830.531518c9.499249 9.499249 24.885227 9.499249 34.384476 0S839.986737 805.623764 830.486464 796.124515zM442.366829 698.401131c-141.380814 0-255.989248-114.631985-255.989248-255.989248 0-141.403341 114.608434-255.989248 255.989248-255.989248 141.37979 0 255.989248 114.585907 255.989248 255.989248C698.356077 583.769146 583.747643 698.401131 442.366829 698.401131z" p-id="29106" fill="#ffffff"></path></svg>
                    </span>
                </div>
                <input type="text" v-model="word">
            </div>
            <router-link tag="div" to="./index" class="search-button"><span>取消</span></router-link>
        </div>

        <!-- 搜索历史 -->
        <div class="search-history" v-show="!word">
            <div class="history-header">
                <span>大家都在搜</span>
            </div>
        </div>

        <!-- 搜索热词 -->
        <div class="hot-word" v-show="!word">
            <ul v-if="hotwordlist">
                <li v-for="(item,index) in hotwordlist" :key="index" @click="tapWord(item.name)">{{item.name}}</li>
            </ul>
        </div>

        <!-- 搜索结果 -->
        <div class="search-text" v-if="searchlist">
            <ol class="book-ol book-ol-normal" v-show="word">
                <li class="book-li" v-for="(item,index) in searchlist" :key="index" v-show="item.cover" @click="bookClick(item.bookId)">
                    <div class="book-layout">
                        <img :src="item.cover" class="book-cover" alt="庆余年2天下太平">
                        <div class="book-cell">
                            <h4 class="book-title">{{item.title}}</h4>
                            <p class="book-desc">{{item.summary}}</p>
                            <div class="book-meta">
                                <div class="book-meta-l">
                                    <span class="book-author book-author-gray">{{item.author}}</span>
                                </div>
                                <div class="book-meta-r">
                                    <span class="tag-small-group origin-right">
                                        <p class="tag-small lightgreen">{{item.category}}</p>
                                        <p class="tag-small lightblue">连载中</p>
                                        <p class="tag-small smallblue">95.73万</p>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </li>
            </ol>
        </div>

    </div>
</template>

<script>
import {getHot,getSearch} from "../api/search"
export default {
    data:function(){
        return {
            word:null,
            hotwordlist:null,
            searchlist:null,
        }
    },
    methods:{
        tapWord(text){
            this.word=text
        },
        bookClick(id){
            this.$emit('get-book-id',id)
            // console.log('id',id)
            this.$router.push({name:'bookdetail',params:{ids:id}})
        }
    },
    created(){
        getHot().then(data=>{
            // console.log(data)
            this.hotwordlist=data.list

        })
    },
    watch:{
        word:function(){
            // console.log(this.word)
            getSearch({search:this.word}).then(data=>{
                // console.log('搜索结果',data)
                this.searchlist=data.data.books
            })
        }
    }
    
}
</script>

<style lang="less" scoped>
.search-from{
    width: 100%;
    height: 44px;
    background-color: red;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search-input{
        width: 280px;
        height: 32px;
        margin-left: 20px;
        display: flex;
        align-items: center;
        border-radius: 32px;
        background-color: #D35B54;
        .search-icon{
            margin-left: 5px;
        }
        input{
            display: block;
            border: none;
            width: 240px;
            height: 20px;
            background-color: #D35B54;
        }
    }
    .search-button{
        margin-right: 20px;
        color: white;
    }
}

.search-history{
    width: 100%;
    height: 44px;
    background-color: rgba(0, 0, 0, 0.03);
    .history-header{
        line-height: 44px;
        padding-left: 16px;
    }
}

.hot-word{
    width: 100%;
    ul{
        width: 95%;
        margin: auto;
        display: flex;
        flex-wrap: wrap;
        li{
            margin: 8px 8px;
            color: #808080;
            border: 1px solid #808080;
            border-radius: 20px;
            padding: 5px 5px;
            font-size: 0.8125rem;
        }
    }
}

.search-text{
    .book-ol{
        .book-li{
            background-color: white;
            .book-layout {
                display: block;
                padding: 1rem;
                position: relative;
                overflow: hidden;
                -webkit-transition: padding-left .15s;
                transition: padding-left .15s;
                .book-cover {
                    width: 4.125rem;
                    height: 5.5rem;
                    float: left;
                    margin-right: .5rem;
                    box-shadow: 0 4px 8px 0 rgba(51, 55, 61, 0.1);
                    border-radius: 2px;
                    font-size: 0;
                }
                .book-cell {
                    overflow: hidden;
                    .book-title {
                        line-height: 1.4;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                        overflow: hidden;
                        font-size: 0.875rem;
                    }
                    .book-desc {
                        line-height: 1.1875rem;
                        height: 2.25rem;
                        font-size: 0.75rem;
                        margin-bottom: .6rem;
                        color: #808080;
                        overflow: hidden;
                    }
                    .book-meta {
                        font-size: .75rem;
                        overflow: hidden;
                        .book-meta-l {
                            float: left;
                            .book-author {
                                display: block;
                                color: #808080;
                                white-space: nowrap;
                                text-overflow: ellipsis;
                                max-width: 10em;
                                max-width: calc(100vw - 2rem - (176rem / 16));
                                font-size: 0.75rem;
                                overflow: hidden;
                            }
                        }
                        .book-meta-r {
                            float: right;
                            position: relative;
                            top: 0.05rem;
                            .tag-small-group {
                                display: inline-block;
                                vertical-align: middle;
                                position: absolute;
                                top: 0.04rem;
                                right: 0;
                                white-space: nowrap;
                                .tag-small {
                                    display: inline-block;
                                    line-height: 10px;
                                    padding: 0.25em 0.25em;
                                    margin: 0 0.15em;
                                    font-size: 10px;
                                    transform: scale(1);
                                    vertical-align: bottom;
                                    border: 1px solid #F16299;
                                }
                                .lightgreen {
                                    color: #F16299;
                                }   
                                .lightblue {
                                    color: #F69A48;
                                }
                                .smallblue {
                                    color: #65B1F5;
                                }
                                p{
                                    font-size: 5px;
                                    font-style: normal;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>
